/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entities;

import beans.CartDAO;
import beans.ClientDAO;
import beans.MessageBean;
import beans.OrderDAO;
import beans.SessionManager;
import beans.TestDao;
import beans.exceptions.NonexistentEntityException;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "CLIENTS")
@NamedQueries({
    @NamedQuery(name = "getAllClients",
    query = "SELECT c FROM Client c"),
    @NamedQuery(name = "removeClientForName",
    query = "DELETE FROM Client a WHERE a.name = :clientName AND a.surname = :clientSurname"),
    @NamedQuery(name = "removeClientForId",
    query = "DELETE FROM Client a WHERE a.id = :clientID"),
    @NamedQuery(name = "getClientForLogin",
    query = "SELECT c FROM Client c WHERE c.login = :clientLogin AND c.password = :clientPassword")
})
public class Client implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID", nullable = false)
    private Long id;

    @Column(name="NAME")
    private String name;

    @Column(name="SURNAME")
    private String surname;

    @Column(name="ADDRESS")
    private String address;

    @Column(name="LOGIN")
    private String login;

    @Column(name="PASSWORD")
    private String password;

    @OneToOne
    @JoinColumn(name="CART_ID")
    private Cart cart;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "client")
    private Collection<Orderr> orders;

    public Client() {
    }

    public Client(String name, String surname, String address) {
        this.name = name;
        this.surname = surname;
        this.address = address;
    }

    public Client(String name, String surname, String address, String login, String password) {
        this.name = name;
        this.surname = surname;
        this.address = address;
        this.login = login;
        this.password = password;
    }


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getAddress() {
        return address;
    }

    public String getName() {
        return name;
    }

    public String getSurname() {
        return surname;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setCart(Cart cart) {
        this.cart = cart;
    }

    public Cart getCart()
    {
        // poprawic
        //return new Cart(this);
        return this.cart;
    }

    public Collection<Orderr> getOrders() {
        return orders;
    }

    public void setOrders(Collection<Orderr> orders) {
        this.orders = orders;
    }

    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Client)) {
            return false;
        }
        Client other = (Client) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "entities.Client[id=" + id + "]";
    }

    public String logout()
    {
        //SessionManager sm = new SessionManager();
        SessionManager.getInstance().clearSessionItem("client");
        return "index";
    }

    public boolean getIsLogged()
    {
        if(name != null)
            return true;
        else return false;
    }

    public void makeOrder() throws NonexistentEntityException, Exception {
        Orderr o = new Orderr();
        //SessionManager sm = new SessionManager();
        o.setClient(this);
        Collection<Book> books = cart.getBooks();
        if(books.size() > 0){
        for (Book b: books) {
            o.getBooks().add(b);
        }
        OrderDAO orderDao = new OrderDAO();
        orderDao.create(o);
        clearCart();
        SessionManager.getInstance().setRequestItem("messageBean", new MessageBean("Zamówienie zostało przyjęte do realizacji"));
        }
        else SessionManager.getInstance().setRequestItem("messageBean", new MessageBean("Aby złożyć zamówienie musisz wybrać książki"));

        
        System.out.println("Stworzono zamowienie");
    }

    public void clearCart() throws NonexistentEntityException, Exception {
        cart.getBooks().clear();
        new CartDAO().edit(cart);
    }
    public String getFullName()
    {
        return name + " " + surname;
    }
}
